package www.reagnetbox.com.view;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.core.CenterPopupView;


import www.reagnetbox.com.R;
import www.reagnetbox.com.bean.User;
import www.reagnetbox.com.callback.SimpleCallback;
import www.reagnetbox.com.util.LitePalUtils;
import www.reagnetbox.com.util.XPopupUtil;

public class EditUserPopup extends CenterPopupView {
    Context context;
    SimpleCallback callback;
    User user;
    Boolean isEditInfo;
    private EditText etUsername;
    private EditText etPwd;

    public EditUserPopup(Context context, User user, Boolean isEditInfo, SimpleCallback callback) {
        super(context);
        this.context = context;
        this.user = user;
        this.isEditInfo = isEditInfo;
        this.callback = callback;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_edit_user;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        boolean isAddUser = user == null;

        ((TextView) findViewById(R.id.tv_title)).setText(isAddUser ? "新增用戶" : (isEditInfo ? "修改用戶信息" : "修改密码"));

        etUsername = findViewById(R.id.et_username);
        etPwd = findViewById(R.id.et_pwd);
        EditText etName = findViewById(R.id.et_name);
        EditText etIdentityNo = findViewById(R.id.et_identity_no);
        EditText etNo = findViewById(R.id.et_no);
        EditText etDept = findViewById(R.id.et_dept);
        LinearLayout llPwd = findViewById(R.id.ll_pwd);
        LinearLayout llInfo1 = findViewById(R.id.ll_info_1);
        LinearLayout llInfo2 = findViewById(R.id.ll_info_2);
        if (!isAddUser) {
            if (isEditInfo) {
                llPwd.setVisibility(View.GONE);
                etName.setText(user.getName());
                etIdentityNo.setText(user.getIdentityNo());
                etNo.setText(user.getNo());
                etDept.setText(user.getDept());
            } else {
                llInfo1.setVisibility(View.GONE);
                llInfo2.setVisibility(View.GONE);
                etUsername.setText(user.getUsername());
                etUsername.setEnabled(false);
            }
        }

        findViewById(R.id.tv_back).setOnClickListener(v -> dismiss());
        findViewById(R.id.tv_save).setOnClickListener(v -> {
            XPopupUtil.showConfirmPopup(context, "提示",
                    "确定" + (isAddUser ? "新增该用户?" : (isEditInfo ? "修改该用户信息?" : "修改密码?")), () -> {
                if (isAddUser) {
                   if (checkRequire()){
                       LitePalUtils.saveUser(
                               etUsername.getText().toString(),
                               etPwd.getText().toString(),
                               etName.getText().toString(),
                               etIdentityNo.getText().toString(),
                               etNo.getText().toString(),
                               etDept.getText().toString()
                       );
                   }
                } else {
                    if (isEditInfo) {
                        LitePalUtils.updateUserInfo(
                                user.getId(),
                                etName.getText().toString(),
                                etIdentityNo.getText().toString(),
                                etNo.getText().toString(),
                                etDept.getText().toString());
                    } else {
                        if (checkRequire()) {
                            LitePalUtils.updateUserPwd(
                                    user.getId(),
                                    etPwd.getText().toString());
                        }
                    }
                }
                ToastUtils.showShort("操作成功");
                dismiss();
                callback.fun();
            });
        });
    }

    private boolean checkRequire(){
        if (TextUtils.isEmpty(etUsername.getText())) {
            XPopupUtil.showKnowPopup(context, "用户名不能为空");
            return false;
        }
        if (TextUtils.isEmpty(etUsername.getText())) {
            XPopupUtil.showKnowPopup(context, "密码不能为空");
            return false;
        }
        return true;
    }
}
